package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.ProfessionDTO;
import com.night.icm.service.base.service.ProfessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * SpecialtyController
 *
 * @author Liao ZhiYong
 * @date 2019/12/21
 */
@Api(tags = {"专业管理"})
@RestController
@RequestMapping("/api/icm-profession")
public class ProfessionController {

    private final ProfessionService professionService;

    @Autowired
    public ProfessionController(ProfessionService professionService) {
        this.professionService = professionService;
    }


    @ApiOperation(value = "分页查询专业信息")
    @GetMapping(value = "")
    public PageDataDTO<ProfessionDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "专业代码") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "专业名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "所属学院") @RequestParam(value = "academyId", required = false) String academyId
    ) {
        return this.professionService.findOnePage(page, size, sort, code, name, academyId);
    }

    @ApiOperation(value = "新增专业")
    @PostMapping(value = "")
    public ProfessionDTO create(
            @ApiParam(value = "专业信息", required = true) @RequestBody ProfessionDTO professionDTO
    ) {
        return this.professionService.create(professionDTO);
    }

    @ApiOperation(value = "删除专业")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "专业id", required = true) @PathVariable(name = "id") String id) {
        this.professionService.deleteById(id);
    }

    @ApiOperation(value = "更新专业信息")
    @PutMapping(value = "/{id}")
    public ProfessionDTO update(
            @ApiParam(value = "专业id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "专业信息", required = true) @RequestBody @Validated ProfessionDTO professionDTO
    ) {
        return this.professionService.update(id, professionDTO);
    }

    @ApiOperation(value = "根据id查找专业信息")
    @GetMapping(value = "/{id}")
    public ProfessionDTO findById(@ApiParam(value = "专业id", required = true) @PathVariable(name = "id") String id,
                                  @ApiParam(value = "是否获取班级", required = true) @RequestParam(value = "isGetClass") Boolean isGetClass) {
        return this.professionService.findById(id, isGetClass);
    }

    @ApiOperation(value = "查找所有专业")
    @GetMapping(value = "/all")
    public List<ProfessionDTO> findAll() {
        return this.professionService.findAll();
    }

    @ApiOperation(value = "查找所有专业并排序")
    @GetMapping(value = "/all/sort")
    public List<ProfessionDTO> findAllBySort(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.professionService.findAllBySort(param, pattern);
    }

    @ApiOperation(value = "检查专业code是否已存在")
    @GetMapping(value = "/checkCodeExist")
    public SimpleDataDTO<Boolean> checkCodeExist(@ApiParam(value = "专业编号", required = true) @RequestParam(value = "code") String code) {
        return this.professionService.checkCodeExist(code);
    }

    @ApiOperation(value = "统计专业数量")
    @GetMapping(value = "/count")
    public Long countProfession() {
        return this.professionService.countProfession();
    }
}